#!/bin/bash

# 检查 can-utils 是否安装
if ! command -v cansend &> /dev/null || ! command -v candump &> /dev/null; then
    echo "can-utils 未安装，请先安装：sudo apt install can-utils"
    exit 1
fi

# 定义日志文件路径
LOG_FILE="./log/can_rx.log"

# 激活并配置CAN接口
sudo ip link set can1 up type can bitrate 800000

# can1接收，并检查是否有can1 11 22 33 44
while true; do
    # 使用timeout命令限制candump的执行时间，例如设置为1秒
    if timeout 1 candump can1 | grep -q "11 22 33 44"; then
        echo "$(date) - Received message on can1: 11 22 33 44" | tee -a "$LOG_FILE"
    else
        echo "$(date) - No message received, retrying..." | tee -a "$LOG_FILE"
    fi
    sleep 0.5
done
